<?
	function processRpc($singleRequest, $dirs) {
	    $hasFile = false;

        foreach($dirs as $dir)
        {
            $tmp = $dir . $singleRequest->action . '_controller.php';

            if(is_file($tmp))
            {
                $hasFile = true;
                require_once($tmp);
            }
        }

        if($hasFile)
        {
            $className = $singleRequest->action . 'Controller';

            $class = new $className(array(), array());

            return call_user_func_array(
                array($class, $singleRequest->method),
                is_array($singleRequest->data) ? $singleRequest->data : array()
            );

        }
	}

	if(!empty($HTTP_RAW_POST_DATA)){
		$request = json_decode($HTTP_RAW_POST_DATA);

		if (!is_array($request)) {
			$request = array($request);
		}

		foreach ($request as $index => $singleRequest) {
			$response[$index] = array(
				'tid' => $singleRequest->tid,
				'action' => $singleRequest->action,
				'method' => $singleRequest->method
			);

			try
			{
				$response[$index]['type'] = 'rpc';
				$response[$index]['result'] = processRpc($singleRequest, $dirs);
			}
			catch (Exception $exception)
			{
				$response[$index]['type'] = 'exception';
				$response[$index]['message'] = $exception->getMessage();
				$response[$index]['where'] = $exception->getTraceAsString();
			}
		}

		print json_encode($response);
		exit;
	}
?>